//
//  AUEssenceTopicVC.swift
//  AwesomeUHomeModule_Example
//
//  Created by Miss_Special on 2018/4/16.
//  Copyright © 2018 CocoaPods. All rights reserved.
//

import UIKit

enum AUEssenceTopicType: String {
    // 无
    case none = ""
    // 推荐
    case recommend = "http://s.budejie.com/topic/list/jingxuan/1/bs0315-iphone-4.5.6/0-20.json"
    // 随听
    case listen = "http://s.budejie.com/topic/list/zuixin/31/bs0315-iphone-4.5.6/0-20.json"
    // 视频
    case video = "http://s.budejie.com/topic/list/jingxuan/41/bs0315-iphone-4.5.6/0-20.json"
    // 图片
    case picture = "http://s.budejie.com/topic/list/jingxuan/10/bs0315-iphone-4.5.6/0-20.json"
    // 笑话
    case joke = "https://s.budejie.com/topic/tag-topic/64/hot/bs0315-iphone-4.5.6/0-20.json"
    // 排行
    case rank = "http://s.budejie.com/topic/list/remen/1/bs0315-iphone-4.5.6/0-20.json"
}

class AUEssenceTopicVC: UIViewController {
    
    fileprivate let viewModel = AUEssenceTopicViewModel()
    
    @IBOutlet weak var tableView: UITableView!
    
    var topicType: AUEssenceTopicType = .none {
        didSet {
            viewModel.requestUrl = topicType.rawValue
        }
    }
    
    // 从xib中加载
    init() {
        let bundle = Bundle(for: type(of: self))
        super.init(nibName: "AUEssenceTopicVC", bundle: bundle)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
        setupUI()
        
        setupTableView()
        
        viewModel.requestData()
        
        viewModel.needRefresh = { [weak self] in
            print("完整刷新tableView")
            self?.tableView.reloadData()
        }
    }
}

extension AUEssenceTopicVC {
    
    fileprivate func setupUI() {
        
    }
    
    fileprivate func setupTableView() {
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
        viewModel.registerCell(tableView)
    }
}

extension AUEssenceTopicVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.tableView(tableView, numberOfRowsInSection: section)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return viewModel.tableView(tableView, cellForRowAt: indexPath)
    }
}

extension AUEssenceTopicVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("外部点击")
        let testVC = AUHomeViewController()
        navigationController?.pushViewController(testVC, animated: true)
    }
}
